<?php
include("../functions.php");
include("../libs/smarty.class.php");

$smarty = new Smarty();

if(isset($_POST['login']))
    {
        $email = $_POST['email'];
        $password = md5($_POST['password']);
        if(!empty($password) && !empty($email)){
            $table = $_POST['choose'];
            if($table=='student'){
                $table = 'user_student';
            }else{
                $table = 'user_company';
            }

            $sql=mysql_query("SELECT id FROM `$table` WHERE email='$email' and password='$password'");
            $row=mysql_fetch_array($sql);
            $count=mysql_num_rows($sql);
            $id = $row['id'];

            if($count == 1)
                {
                session_register("user");
                $_SESSION['user_email'] = $email;
                $_SESSION['user_id'] = $id;
                $_SESSION['user_type'] = $table;
                if($table == 'user_company'){
                    $company = array();
                    $company = getRow("id,email,name,address,phone,site","$id","$table");
                    $_SESSION['company_info'] = $company;
                }
                if($table =='user_student'){
                    $student = array();
                    $student = getRow("id,email,name,surname,album_nr,faculty_id,specialization_id,practice_id","$id","$table");
                    $_SESSION['student_info'] = $student;
                }
                header( 'Location: ../index.php?info=22' );
            }
            else{
                header( 'Location: ../index.php?error=7' );
            }
        }else{
             header( 'Location: ../index.php?error=21' );
        }
    }
?>
